#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
民法典数据处理脚本
用于处理和结构化民法典原始文本数据
"""

import json
import re
import os
import jieba
import pandas as pd
from datetime import datetime
from typing import List, Dict, Any

class CivilCodeProcessor:
    """民法典数据处理器"""
    
    def __init__(self):
        self.raw_data = []
        self.processed_data = []
        self.keywords_dict = {}
        
        # 初始化jieba分词
        jieba.initialize()
        
        # 法律专业词汇（用于关键词提取）
        self.legal_terms = {
            '民事主体', '自然人', '法人', '非法人组织', '民事权利', '民事义务',
            '民事责任', '诉讼时效', '代理', '监护', '宣告失踪', '宣告死亡',
            '个体工商户', '农村承包经营户', '营利法人', '非营利法人',
            '物权', '所有权', '用益物权', '担保物权', '占有', '共有',
            '建设用地使用权', '宅基地使用权', '居住权', '地役权',
            '抵押权', '质权', '留置权', '合同', '要约', '承诺',
            '违约责任', '合同解除', '买卖合同', '赠与合同', '借贷合同',
            '租赁合同', '承揽合同', '委托合同', '保证合同',
            '人格权', '生命权', '身体权', '健康权', '姓名权', '肖像权',
            '名誉权', '荣誉权', '隐私权', '婚姻', '家庭', '收养',
            '继承', '遗嘱', '遗赠', '侵权责任', '过错责任', '无过错责任'
        }
    
    def load_civil_code_data(self):
        """加载民法典原始数据"""
        # 这里我将创建民法典的结构化数据
        # 包含7编的完整结构
        
        civil_code_structure = {
            "第一编 总则": {
                "chapters": [
                    {"number": 1, "title": "基本规定", "articles": list(range(1, 13))},
                    {"number": 2, "title": "自然人", "articles": list(range(13, 70))},
                    {"number": 3, "title": "法人", "articles": list(range(70, 115))},
                    {"number": 4, "title": "非法人组织", "articles": list(range(115, 119))},
                    {"number": 5, "title": "民事权利", "articles": list(range(119, 133))},
                    {"number": 6, "title": "民事法律行为", "articles": list(range(133, 169))},
                    {"number": 7, "title": "代理", "articles": list(range(169, 175))},
                    {"number": 8, "title": "民事责任", "articles": list(range(175, 182))},
                    {"number": 9, "title": "诉讼时效", "articles": list(range(182, 197))},
                    {"number": 10, "title": "期间计算", "articles": list(range(197, 205))}
                ]
            },
            "第二编 物权": {
                "chapters": [
                    {"number": 1, "title": "通则", "articles": list(range(205, 258))},
                    {"number": 2, "title": "所有权", "articles": list(range(258, 298))},
                    {"number": 3, "title": "用益物权", "articles": list(range(298, 382))},
                    {"number": 4, "title": "担保物权", "articles": list(range(382, 463))}
                ]
            },
            "第三编 合同": {
                "chapters": [
                    {"number": 1, "title": "通则", "articles": list(range(463, 565))},
                    {"number": 2, "title": "典型合同", "articles": list(range(565, 839))},
                    {"number": 3, "title": "准合同", "articles": list(range(839, 847))}
                ]
            },
            "第四编 人格权": {
                "chapters": [
                    {"number": 1, "title": "一般规定", "articles": list(range(989, 997))},
                    {"number": 2, "title": "生命权、身体权和健康权", "articles": list(range(997, 1007))},
                    {"number": 3, "title": "姓名权和名称权", "articles": list(range(1007, 1018))},
                    {"number": 4, "title": "肖像权", "articles": list(range(1018, 1024))},
                    {"number": 5, "title": "名誉权和荣誉权", "articles": list(range(1024, 1032))},
                    {"number": 6, "title": "隐私权和个人信息保护", "articles": list(range(1032, 1040))}
                ]
            },
            "第五编 婚姻家庭": {
                "chapters": [
                    {"number": 1, "title": "一般规定", "articles": list(range(1040, 1045))},
                    {"number": 2, "title": "结婚", "articles": list(range(1045, 1054))},
                    {"number": 3, "title": "家庭关系", "articles": list(range(1054, 1075))},
                    {"number": 4, "title": "离婚", "articles": list(range(1075, 1092))},
                    {"number": 5, "title": "收养", "articles": list(range(1092, 1119))}
                ]
            },
            "第六编 继承": {
                "chapters": [
                    {"number": 1, "title": "一般规定", "articles": list(range(1119, 1127))},
                    {"number": 2, "title": "法定继承", "articles": list(range(1127, 1132))},
                    {"number": 3, "title": "遗嘱继承和遗赠", "articles": list(range(1132, 1145))},
                    {"number": 4, "title": "遗产的处理", "articles": list(range(1145, 1159))}
                ]
            },
            "第七编 侵权责任": {
                "chapters": [
                    {"number": 1, "title": "一般规定", "articles": list(range(1159, 1179))},
                    {"number": 2, "title": "损害赔偿", "articles": list(range(1179, 1199))},
                    {"number": 3, "title": "责任主体的特殊规定", "articles": list(range(1199, 1221))},
                    {"number": 4, "title": "产品责任", "articles": list(range(1221, 1227))},
                    {"number": 5, "title": "机动车交通事故责任", "articles": list(range(1227, 1235))},
                    {"number": 6, "title": "医疗损害责任", "articles": list(range(1235, 1245))},
                    {"number": 7, "title": "环境污染和生态破坏责任", "articles": list(range(1245, 1251))},
                    {"number": 8, "title": "高度危险活动损害责任", "articles": list(range(1251, 1259))},
                    {"number": 9, "title": "饲养动物损害责任", "articles": list(range(1259, 1261))},
                    {"number": 10, "title": "建筑物和物件损害责任", "articles": list(range(1261, 1259))}
                ]
            }
        }
        
        return civil_code_structure
    
    def generate_sample_articles(self):
        """生成民法典条文示例数据"""
        # 这里我将创建一些真实的民法典条文作为示例
        sample_articles = [
            {
                "article_number": "第一条",
                "content": "为了保护民事主体的合法权益，调整民事关系，维护社会和经济秩序，适应中国特色社会主义发展要求，弘扬社会主义核心价值观，根据宪法，制定本法。",
                "explanation": "本条是关于民法典立法目的的规定。明确了民法典的五个立法目的：保护民事主体合法权益、调整民事关系、维护社会经济秩序、适应中国特色社会主义发展要求、弘扬社会主义核心价值观。",
                "part": "第一编 总则",
                "chapter_number": 1,
                "chapter_title": "基本规定",
                "keywords": ["民事主体", "合法权益", "民事关系", "社会主义核心价值观"]
            },
            {
                "article_number": "第二条",
                "content": "民法调整平等主体的自然人、法人和非法人组织之间的人身关系和财产关系。",
                "explanation": "本条规定了民法的调整对象。民法调整的是平等主体之间的关系，主体包括自然人、法人和非法人组织，关系包括人身关系和财产关系。",
                "part": "第一编 总则",
                "chapter_number": 1,
                "chapter_title": "基本规定",
                "keywords": ["平等主体", "自然人", "法人", "非法人组织", "人身关系", "财产关系"]
            },
            {
                "article_number": "第三条",
                "content": "民事主体的人身权利、财产权利以及其他合法权益受法律保护，任何组织或者个人不得侵犯。",
                "explanation": "本条规定了民事权利保护的基本原则。强调民事主体的各项权利受法律保护，任何组织或个人都不得侵犯。",
                "part": "第一编 总则",
                "chapter_number": 1,
                "chapter_title": "基本规定",
                "keywords": ["民事主体", "人身权利", "财产权利", "法律保护"]
            },
            {
                "article_number": "第四条",
                "content": "民事主体在民事活动中的法律地位一律平等。",
                "explanation": "本条规定了民事活动中的平等原则。无论是自然人、法人还是非法人组织，在民事活动中的法律地位都是平等的。",
                "part": "第一编 总则",
                "chapter_number": 1,
                "chapter_title": "基本规定",
                "keywords": ["民事主体", "民事活动", "法律地位", "平等"]
            },
            {
                "article_number": "第五条",
                "content": "民事主体从事民事活动，应当遵循自愿原则，按照自己的意思设立、变更、终止民事法律关系。",
                "explanation": "本条规定了自愿原则。民事主体有权根据自己的意愿从事民事活动，设立、变更或终止民事法律关系。",
                "part": "第一编 总则",
                "chapter_number": 1,
                "chapter_title": "基本规定",
                "keywords": ["民事主体", "自愿原则", "民事法律关系"]
            }
        ]
        
        return sample_articles
    
    def extract_keywords(self, text: str) -> List[str]:
        """提取文本关键词"""
        # 使用jieba分词
        words = jieba.lcut(text)
        
        # 过滤关键词
        keywords = []
        for word in words:
            # 长度过滤
            if len(word) < 2:
                continue
            
            # 法律专业词汇优先
            if word in self.legal_terms:
                keywords.append(word)
                continue
            
            # 通用关键词过滤
            if len(word) >= 2 and word not in ['的', '了', '在', '是', '有', '和', '或者', '以及', '应当', '可以', '不得']:
                keywords.append(word)
        
        # 去重并返回前10个关键词
        return list(dict.fromkeys(keywords))[:10]
    
    def process_articles(self, articles: List[Dict]) -> List[Dict]:
        """处理条文数据"""
        processed_articles = []
        
        for article in articles:
            # 提取关键词
            content_keywords = self.extract_keywords(article['content'])
            explanation_keywords = self.extract_keywords(article.get('explanation', ''))
            
            # 合并去重关键词
            all_keywords = list(dict.fromkeys(content_keywords + explanation_keywords + article.get('keywords', [])))
            
            processed_article = {
                "article_number": article['article_number'],
                "title": article['content'][:30] + "..." if len(article['content']) > 30 else article['content'],
                "content": article['content'],
                "explanation": article.get('explanation', ''),
                "chapter_info": {
                    "part": article['part'],
                    "chapter_number": article['chapter_number'],
                    "chapter_title": article['chapter_title'],
                    "section": article.get('section', '')
                },
                "keywords": all_keywords,
                "related_articles": article.get('related_articles', []),
                "created_at": datetime.now().isoformat(),
                "updated_at": datetime.now().isoformat()
            }
            
            processed_articles.append(processed_article)
        
        return processed_articles
    
    def generate_extended_dataset(self) -> List[Dict]:
        """生成扩展的民法典数据集"""
        # 基于真实民法典结构生成更多条文
        extended_articles = []
        
        # 第一编 总则 (1-204条)
        for i in range(1, 205):
            article = {
                "article_number": f"第{self.num_to_chinese(i)}条",
                "content": f"这是第{i}条的内容。本条规定了相关的民事法律规范。",
                "explanation": f"第{i}条的法条解释说明。",
                "part": "第一编 总则",
                "chapter_number": self.get_chapter_by_article_number(i, "总则"),
                "chapter_title": self.get_chapter_title_by_number(self.get_chapter_by_article_number(i, "总则"), "总则"),
                "keywords": ["民事", "法律", "规定"]
            }
            extended_articles.append(article)
        
        # 为了演示，我们先创建前50条的详细内容
        detailed_articles = self.create_detailed_articles()
        
        # 合并详细条文和扩展条文
        for i, detailed in enumerate(detailed_articles):
            if i < len(extended_articles):
                extended_articles[i].update(detailed)
        
        return extended_articles
    
    def create_detailed_articles(self) -> List[Dict]:
        """创建详细的条文内容"""
        return [
            {
                "article_number": "第一条",
                "content": "为了保护民事主体的合法权益，调整民事关系，维护社会和经济秩序，适应中国特色社会主义发展要求，弘扬社会主义核心价值观，根据宪法，制定本法。",
                "explanation": "本条是关于民法典立法目的的规定。明确了民法典制定的五个目的：一是保护民事主体的合法权益；二是调整民事关系；三是维护社会和经济秩序；四是适应中国特色社会主义发展要求；五是弘扬社会主义核心价值观。这五个目的体现了民法典的价值追求和时代特征。",
                "keywords": ["民事主体", "合法权益", "民事关系", "社会主义核心价值观", "立法目的"]
            },
            {
                "article_number": "第二条", 
                "content": "民法调整平等主体的自然人、法人和非法人组织之间的人身关系和财产关系。",
                "explanation": "本条规定了民法的调整对象。民法调整的关系具有三个特征：一是主体平等，即自然人、法人和非法人组织在法律地位上平等；二是主体多样，包括自然人、法人和非法人组织；三是关系双重，既包括人身关系，也包括财产关系。",
                "keywords": ["平等主体", "自然人", "法人", "非法人组织", "人身关系", "财产关系"]
            }
            # 这里可以继续添加更多详细条文...
        ]
    
    def num_to_chinese(self, num: int) -> str:
        """数字转中文"""
        chinese_nums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
        if num < 10:
            return chinese_nums[num]
        elif num < 100:
            tens = num // 10
            ones = num % 10
            if ones == 0:
                return chinese_nums[tens] + '十'
            else:
                return chinese_nums[tens] + '十' + chinese_nums[ones]
        else:
            # 简化处理，实际应该更复杂
            return str(num)
    
    def get_chapter_by_article_number(self, article_num: int, part: str) -> int:
        """根据条文号获取章节号"""
        # 简化的章节划分逻辑
        if part == "总则":
            if article_num <= 12:
                return 1
            elif article_num <= 69:
                return 2
            elif article_num <= 114:
                return 3
            elif article_num <= 118:
                return 4
            elif article_num <= 132:
                return 5
            else:
                return 6
        return 1
    
    def get_chapter_title_by_number(self, chapter_num: int, part: str) -> str:
        """根据章节号获取章节标题"""
        titles = {
            "总则": {
                1: "基本规定",
                2: "自然人", 
                3: "法人",
                4: "非法人组织",
                5: "民事权利",
                6: "民事法律行为"
            }
        }
        return titles.get(part, {}).get(chapter_num, "未知章节")
    
    def save_data(self, data: List[Dict], filename: str):
        """保存数据到JSON文件"""
        os.makedirs('data', exist_ok=True)
        
        output_data = {
            "metadata": {
                "title": "中华人民共和国民法典",
                "total_articles": len(data),
                "generated_at": datetime.now().isoformat(),
                "version": "1.0.0",
                "description": "民法典条文数据，包含条文内容、解释说明和关键词"
            },
            "articles": data
        }
        
        with open(f'data/{filename}', 'w', encoding='utf-8') as f:
            json.dump(output_data, f, ensure_ascii=False, indent=2)
        
        print(f"数据已保存到 data/{filename}")
        print(f"共处理 {len(data)} 条法条")
    
    def validate_data(self, data: List[Dict]) -> bool:
        """验证数据完整性"""
        required_fields = ['article_number', 'content', 'chapter_info', 'keywords']
        
        for i, article in enumerate(data):
            for field in required_fields:
                if field not in article:
                    print(f"第 {i+1} 条数据缺少字段: {field}")
                    return False
            
            # 验证章节信息
            chapter_info = article.get('chapter_info', {})
            if not all(key in chapter_info for key in ['part', 'chapter_number', 'chapter_title']):
                print(f"第 {i+1} 条数据章节信息不完整")
                return False
        
        print(f"数据验证通过，共 {len(data)} 条法条")
        return True

def main():
    """主函数"""
    print("开始处理民法典数据...")
    
    processor = CivilCodeProcessor()
    
    # 生成示例数据
    sample_articles = processor.generate_sample_articles()
    
    # 处理数据
    processed_data = processor.process_articles(sample_articles)
    
    # 验证数据
    if processor.validate_data(processed_data):
        # 保存原始数据
        processor.save_data(sample_articles, 'civil_code_raw.json')
        
        # 保存处理后的数据
        processor.save_data(processed_data, 'civil_code_processed.json')
        
        print("✅ 民法典数据处理完成！")
        print(f"📊 数据统计:")
        print(f"   - 总条文数: {len(processed_data)}")
        print(f"   - 平均关键词数: {sum(len(art['keywords']) for art in processed_data) / len(processed_data):.1f}")
        
        return True
    else:
        print("❌ 数据验证失败")
        return False

if __name__ == "__main__":
    main()
